import os


# 创建目录树
def create_dir_tree(path: str) -> bool:
    if not os.path.exists(path):
        os.makedirs(path)
        return True

    return False


# 写入文本文件
def write_text_file(content: str, filename: str, encoding='utf-8') -> bool:
    # 目标存在，而且不是文件
    if os.path.exists(filename) and (not os.path.isfile(filename)):
        raise FileNotFoundError(filename)

    file = open(filename, mode='wb')

    try:
        file.write(content.encode(encoding))
    except Exception as err:
        raise err
    finally:
        file.close()

    return True


# 读取文本文件
def read_text_file(filename: str, encoding='utf-8'):
    if not os.path.exists(filename):
        raise FileNotFoundError(filename)

    if not os.path.isfile(filename):
        print('目标不是一个文件: ' + filename)
        return FileNotFoundError(filename)

    content = open(filename, "r", encoding=encoding).read()
    return content
